<template>
    <el-dialog title="修改用户信息" :visible.sync="editDialogVisible" width="50%" @close="editDialogClosed">
            <!-- 内容主体 -->
            <el-form :model="editUserForm" ref="editUserFormRef" :rules="editUserFormRules" label-width="70px">
                <el-form-item label="用户名" prop="mg_name">
                    <el-input v-model="editUserForm.mg_name" disabled></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="mg_email">
                    <el-input v-model="editUserForm.mg_email"></el-input>
                </el-form-item>
                <el-form-item label="手机" prop="mg_mobile">
                    <el-input v-model="editUserForm.mg_mobile"></el-input>
                </el-form-item>
                 <el-form-item label="角色" prop="role_id">
                    <el-select v-model="editUserForm.role_id" placeholder="请选择所属角色">
                    <el-option v-for="item in roleList" :key="item.role_id" :label="item.role_name" :value="item.role_id"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="editUser">确 定</el-button>
            </span>
        </el-dialog>
</template>

<script>
export default {
    name: "editDialog",
    props: {
        editUserForm: Object,
    },
    data() {
        // 自定义邮箱规则
        var checkEmail = (rule, value, callback) => {
            const regEmail = /^\w+@\w+(\.\w+)+$/;
            if (regEmail.test(value)) {
                // 合法邮箱
                return callback();
            }
            callback(new Error("请输入合法邮箱"));
        };
        // 自定义手机号规则
        var checkMobile = (rule, value, callback) => {
            const regMobile = /^1[34578]\d{9}$/;
            if (regMobile.test(value)) {
                return callback();
            }
            // 返回一个错误提示
            callback(new Error("请输入合法的手机号码"));
        };
        return {
            roleList: [],
            // 修改用户
            editDialogVisible: false,
            // 编辑用户表单验证
            editUserFormRules: {
                mg_email: [
                    { required: true, message: "请输入邮箱", trigger: "blur" },
                    { validator: checkEmail, trigger: "blur" },
                ],
                mg_mobile: [
                    {
                        required: true,
                        message: "请输入手机号码",
                        trigger: "blur",
                    },
                    { validator: checkMobile, trigger: "blur" },
                ],
            },
        };
    },
    created() {
        this.getRoleList();
    },
    methods: {
        async getRoleList() {
            const { data: res } = await this.$http.get("/role");
            if (res.status !== 200) {
                return this.$message.error(res.message);
            }
            this.roleList = res.results;
            console.log(this.roleList);
        },
        //显示弹窗
        showDialog() {
            this.editDialogVisible = true;
        },
        // 监听修改用户对话框的关闭事件
        editDialogClosed() {
            this.$refs.editUserFormRef.resetFields();
        },
        // 修改用户信息
        editUser() {
            // 提交请求前，表单预验证
            this.$refs.editUserFormRef.validate(async (valid) => {
                // console.log(valid)
                // 表单预校验失败
                if (!valid) return;
                const { data: res } = await this.$http.put(
                    "manager/" + this.editUserForm.mg_id,
                    this.editUserForm
                );
                if (res.status !== 200) {
                    this.$message.error(res.message);
                }
                this.$message.success(res.message);
                // 隐藏添加用户对话框
                this.editDialogVisible = false;
                this.$emit("update");
            });
        },
    },
};
</script>

